package xupt.se.ttms.view.customer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import xupt.se.ttms.model.Movie;
import xupt.se.ttms.model.Play;
import xupt.se.ttms.model.Studio;
import xupt.se.ttms.service.MovieSrv;
import xupt.se.ttms.service.PlaySrv;
import xupt.se.ttms.service.StudioSrv;
import xupt.se.ttms.view.tmpl.Login;

import xupt.se.ttms.view.seat.Seat_Choose;

public class Customer_MainUITmpl { //信息展示界面

	/**
	 * @param args
	 */
	public static JFrame frame;
	public static String Data[];
	public static JLabel label = new JLabel();
	public static JLabel label1 = new JLabel();
	public static JPanel jlabel  =null;
	public static String args;
	public static Movie infor = new Movie();
	public static JTextField text;
	public Customer_MainUITmpl(){
		
	}
	public Customer_MainUITmpl(String argss) {
		frame = new JFrame();
		args = argss;
		// TODO Auto-generated method stub
		frame.setLayout(null);
		frame.setTitle("顾客");
		ImageIcon icon=new ImageIcon("img/2.png");
		JLabel a = new JLabel(icon);
		frame.setLayout(null);
		icon.setImage(icon.getImage().getScaledInstance(850,70,Image.SCALE_DEFAULT));
		JLabel users = new JLabel("用    户    名:   "+args);
		a.add(users);
		users.setBounds(670,10,150,20);
		JButton btnModPwd = new JButton("修改密码");
		btnModPwd.setFocusPainted(false) ;
		btnModPwd.setBounds(670, 35, 60, 20);
		btnModPwd.setMargin(new Insets(0,0,0,0));
		btnModPwd.setContentAreaFilled(false);
		btnModPwd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				Customer_ChangePassWord a= new Customer_ChangePassWord(args);
			}
		});
		a.add(btnModPwd);
		JButton btnModPwd1 = new JButton("注销");
		btnModPwd1.setFocusPainted(false) ;
		btnModPwd1.setBounds(740, 35, 40, 20);
		btnModPwd1.setMargin(new Insets(0,0,0,0));
		btnModPwd1.setContentAreaFilled(false);
		btnModPwd1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				frame.dispose();
				Login s =new Login();
			}
		});
		a.add(btnModPwd1);
		frame.add(a);
		a.setBounds(0,0,850,70);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        frame.setBounds(0, 0, 850, 500);
        // 窗体大小不能改变
        frame.setResizable(false);
        // 居中显示
        frame.setLocationRelativeTo(null);
        // 窗体可见
        frame.setVisible(true);
	}
	public static void init(){
		jlabel=new JPanel();
		jlabel.setLayout(null);
		jlabel.setPreferredSize(new Dimension(900,250));
		JScrollPane scroll =new  JScrollPane(jlabel);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
		scroll.setBounds(220,105,620,280);
		frame.add(scroll);
		label.setLayout(null);
		label.setBounds(0, 100, 850, 300);
		label1.setLayout(null);
		label1.setBounds(-1, 400, 850, 100);
		label1.setBorder(BorderFactory.createLineBorder(Color.gray));
		frame.add(label1);
		JButton btnModPwd = new JButton("影片信息");
		btnModPwd.setFocusPainted(false) ;
		btnModPwd.setBounds(0, 70, 212, 30);
		btnModPwd.setMargin(new Insets(0,0,0,0));
		btnModPwd.setContentAreaFilled(false);
		frame.add(btnModPwd);
		Data = new String[4];
		Data[0]="最热影片";
		Data[1] = "推荐影片";
		Data[2] = "正在上映";
		Data[3] = "即将上映";
		MovieSrv movieSrv=new MovieSrv();
		Movie movie=new Movie();
		List<Movie> list=new ArrayList<Movie>();
		try {
			
			list=movieSrv.FetchHot((float)2.0);
			Confirms.setMovieTable(list,infor);
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		Side(Data);
		down();
		btnModPwd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				Data = new String[4];
				Data[0]="最热影片";
				Data[1] = "推荐影片";
				Data[2] = "正在上映";
				Data[3] = "即将上映";
				MovieSrv movieSrv=new MovieSrv();
				Movie movie=new Movie();
				List<Movie> list=new ArrayList<Movie>();
				try {
					
					list=movieSrv.FetchHot((float)2.0);
					Confirms.setMovieTable(list,infor);
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				Side(Data);
				down();
			}
		});
		JButton btnModPwd1 = new JButton("影片查询");
		btnModPwd1.setFocusPainted(false) ;
		btnModPwd1.setBounds(212, 70, 212, 30);
		btnModPwd1.setMargin(new Insets(0,0,0,0));
		btnModPwd1.setContentAreaFilled(false);
		frame.add(btnModPwd1);
		btnModPwd1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				Data = new String[2];
				Data[0]="全部影片";
				Data[1] = "热播影片";
				MovieSrv movieSrv=new MovieSrv();
				Movie movie=new Movie();
				List<Movie> list=new ArrayList<Movie>();
				try {
					
					list=movieSrv.FetchAll();
					Confirms.setMovieTable(list,infor);
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				Side(Data);
				down1();
			}
		});
		JButton btnModPwd2 = new JButton("购票");
		btnModPwd2.setFocusPainted(false) ;
		btnModPwd2.setBounds(424, 70, 212, 30);
		btnModPwd2.setMargin(new Insets(0,0,0,0));
		btnModPwd2.setContentAreaFilled(false);
		frame.add(btnModPwd2);
		btnModPwd2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				Data = new String[2];
				Data[0]="单人购票";
				Data[1] = "团体购票";
				Side(Data);
				down();
			}
		});
		JButton btnModPwd3 = new JButton("我的");
		btnModPwd3.setFocusPainted(false) ;
		btnModPwd3.setBounds(636, 70, 212, 30);
		btnModPwd3.setMargin(new Insets(0,0,0,0));
		btnModPwd3.setContentAreaFilled(false);
		frame.add(btnModPwd3);
		btnModPwd3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				Data = new String[3];
				Data[0]="订单";
				Data[1] = "余额";
				Data[2] = "权限";
				Side(Data);
				down2();
			}
		});
	}
	public static void Side(String Date[]){
		int i;
		int t=2;
		label.removeAll();
		for(i=0;i<Date.length;i++){
			JButton btnModPwd = new JButton(Date[i]);
			Confirms aa = new Confirms(infor);
			btnModPwd.setFocusPainted(false) ;
			btnModPwd.setBounds(0, t, 212, 60);
			aa.getData(Date[i]);
			t += 62;
			btnModPwd.setMargin(new Insets(0,0,0,0));
			btnModPwd.setContentAreaFilled(false);
			btnModPwd.addActionListener(aa);
			label.add(btnModPwd);
		}
		frame.add(label);
		frame.repaint();
		
	}
	public static void down(){
		label1.removeAll();
		JButton btnModPwd3 = new JButton("订票");
		btnModPwd3.setFocusPainted(false) ;
		btnModPwd3.setBounds(380, 20, 100, 20);
		btnModPwd3.setMargin(new Insets(0,0,0,0));
		btnModPwd3.setContentAreaFilled(false);
		label1.add(btnModPwd3);
		btnModPwd3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
			
			}
		});
		
		label1.repaint();
	}
	public static void down1(){
		label1.removeAll();
		JLabel lel = new JLabel("查询影片：");
		lel.setBounds(110,15,100,30);
		label1.add(lel);
		text = new JTextField();
		text.setBounds(180,20,150,20);
		label1.add(text);
		JButton btnModPwd2 = new JButton("查询");
		btnModPwd2.setFocusPainted(false) ;
		btnModPwd2.setBounds(350, 20, 75, 20);
		btnModPwd2.setMargin(new Insets(0,0,0,0));
		btnModPwd2.setContentAreaFilled(false);
		btnModPwd2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
		
				MovieSrv movieSrv=null;
				Movie movie=null;
				List<Movie> list=null;
				if(text.getText().equals(""))
				{
					JOptionPane.showConfirmDialog(null, "请输入影片名称");
				}
				else
				{
					movieSrv=new MovieSrv();
					movie=new Movie();
					list=new ArrayList<>();
					try {
						
						//list=movieSrv.Fetch(text.getText());
						list=movieSrv.FetchFuzzy(text.getText());
						Confirms.setMovieTable(list,infor);
						
					} catch (Exception e1) {
						// TODO 自动生成的 catch 块
						e1.printStackTrace();
					}
				}								
			}
		});
		label1.add(btnModPwd2);
		JButton btnModPwd3 = new JButton("订票");
		btnModPwd3.setFocusPainted(false) ;
		btnModPwd3.setBounds(550, 20, 100, 20);
		btnModPwd3.setMargin(new Insets(0,0,0,0));
		btnModPwd3.setContentAreaFilled(false);
		label1.add(btnModPwd3);
		btnModPwd3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent Event) {
				
			}
		});
		
		label1.repaint();
	}
	public static void down2(){
		label1.removeAll();
		
		
		label1.repaint();
	}
	
}
class Confirms implements ActionListener{
	public Movie infor;
	public String Data;
	public static JPanel jlabel;
	public Confirms(Movie infors){
		this.infor = infors;
	}
	public void getData(String Data){
		this.Data = Data;
	}
	public void actionPerformed(ActionEvent e){
		System.out.print(this.infor);
		if(Data.equals("最热影片"))
		{
			MovieSrv movieSrv=new MovieSrv();
			Movie movie=new Movie();
			List<Movie> list=new ArrayList<Movie>();
			try {
				
				list=movieSrv.FetchHot((float)2.0);
				setMovieTable(list,this.infor);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		else  if (Data.equals("正在上映")) {
			MovieSrv movieSrv=new MovieSrv();
			Movie movie=new Movie();
			List<Movie> list=new ArrayList<Movie>();
			try {
				
				list=movieSrv.FetchNew(2015);
				setMovieTable(list,this.infor);
			
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		else  if (Data.equals("推荐影片")) {
			MovieSrv movieSrv=new MovieSrv();
			Movie movie=new Movie();
			List<Movie> list=new ArrayList<Movie>();
			try {
				
				list=movieSrv.FetchRecom(9.0f);
				setMovieTable(list,this.infor);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		else  if (Data.equals("全部影片")) {
			MovieSrv movieSrv=new MovieSrv();
			Movie movie=new Movie();
			List<Movie> list=new ArrayList<Movie>();
			try {
				
				list=movieSrv.FetchAll();
				setMovieTable(list,this.infor);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
		else if(Data.equals("热播影片"))
		{
			MovieSrv movieSrv=new MovieSrv();
			Movie movie=new Movie();
			List<Movie> list=new ArrayList<Movie>();
			try {
				
				list=movieSrv.FetchHot((float)8.5);
				setMovieTable(list,this.infor);
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		}
	}
	public static void setMovieTable(List<Movie> list,Movie infor)
	{
		int i = 30;
		jlabel = Customer_MainUITmpl.jlabel;
		jlabel.removeAll();
		if(list.size()!=0)
		{
			System.out.println(list.get(0).getClass().getSimpleName());
			
			for (Movie movie2 : list) {
				ImageIcon icon=new ImageIcon(String.valueOf(movie2.getImg()));
				JLabel a = new JLabel(icon);
				 Confirm confirm = new Confirm();
		         confirm.getJFrame(movie2);
		         a.addMouseListener(confirm);
				a.setBounds(i, 40, 120, 150);
				i = i+150;
				icon.setImage(icon.getImage().getScaledInstance(100,150,Image.SCALE_DEFAULT));
				jlabel.add(a);
			}
			
		}
		jlabel.repaint();
	}
}
class Confirm implements MouseListener{
	public Movie movie;
	public void getJFrame(Movie movie)
	{
		this.movie = movie;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		
		int mv_id=this.movie.getMv_id();
		PlaySrv playSrv=new PlaySrv();
		List<Play> plays1=new ArrayList<>();
		List<Play> choiceplays=new ArrayList<>();
		StudioSrv studioSrv=new StudioSrv();
		List<Studio> studios=new ArrayList<>();
		//List<String> studio=new ArrayList<>();
		try {
			
			plays1=playSrv.FetchAll();
			if(plays1.size()<1)
			{
				return ;
			}
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}/*
		try {
			studios=studioSrv.FetchAll();
			for  (Studio studio2 : studios) {
				studio.add(studio2.getName());
			}
		} catch (Exception e2) {
			// TODO 自动生成的 catch 块
			e2.printStackTrace();
		}*/
		List<String> studio1=new ArrayList<>();
		List<String> time=new ArrayList<>();
		Map<String, List<String>> map=new HashMap<>();
		for (Play play : plays1) {
			if(play.getMv_id()==mv_id)
			{
				try {
					studios=studioSrv.Fetch(play.getStudio_id());
					if(studios.size()<1)
					{
						return ;
					}
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				String stu=studios.get(0).getName();
				if(map.containsKey(stu))
				{
					time=new ArrayList<>();
					time=map.get(stu);
					time.add(play.getPlay_time());
					map.put(stu, time);
				}
				else
				{
					time=new ArrayList<>();
					studio1.add(stu);
					time.add(play.getPlay_time());
					map.put(stu, time);
				}
			
			}
			
		
		}
		System.out.print("111");
		new Customer_InformationUI(movie,studio1,map);
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO 自动生成的方法存根
		
	}
	
}